package com.cgbl.cg.vm.classification

import android.app.Application
import androidx.databinding.ObservableField
import api.CourseServiceFactory
import api.ResourceServiceFactory
import api.WorkServiceFactory
import com.robot.base.base.vm.GLBaseViewModel
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.model.CourseAllTypeBean
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent
import me.goldze.mvvmhabit.http.BaseResponse

class ClassificationTabViewModel(application: Application) : GLBaseViewModel(application) {
    val list = arrayListOf<String>()
    val typeTab = ObservableField<String>()

    val uc = UiChangeEvent()

    class UiChangeEvent {
        val dataEvent: SingleLiveEvent<MutableList<CourseAllTypeBean>> = SingleLiveEvent()
    }

    val clickList = arrayListOf<CourseAllTypeBean>()

    fun initData(type: String) {
        list.clear()
        when (type) {
            "0" -> {
                fetchCourseClassificationData()
            }
            "1" -> {
                fetchResourceClassificationData()
//                uc.dataEvent.postValue(list)
            }
            "2" -> {
                fetchWorksClassificationData()
            }
        }
    }

    private fun fetchCourseClassificationData() {
        CourseServiceFactory.allType().subscribe(object :
            AbstractViewModelSubscriber<BaseResponse<List<CourseAllTypeBean>>>(this) {
            override fun onNext(t: BaseResponse<List<CourseAllTypeBean>>) {
                uc.dataEvent.postValue(t.data as MutableList<CourseAllTypeBean>?)
            }
        })
    }

    private fun fetchResourceClassificationData() {
        ResourceServiceFactory.allType().subscribe(object :
            AbstractViewModelSubscriber<BaseResponse<List<CourseAllTypeBean>>>(this) {
            override fun onNext(t: BaseResponse<List<CourseAllTypeBean>>) {
                uc.dataEvent.postValue(t.data as MutableList<CourseAllTypeBean>?)
            }
        })
    }

    private fun fetchWorksClassificationData() {
        WorkServiceFactory.allType().subscribe(object :
            AbstractViewModelSubscriber<BaseResponse<List<CourseAllTypeBean>>>(this) {
            override fun onNext(t: BaseResponse<List<CourseAllTypeBean>>) {
                uc.dataEvent.postValue(t.data as MutableList<CourseAllTypeBean>?)
            }
        })
    }
}